window.onload=function() {
    // 账户设置隐藏显示
    // 第一页
    var header_hidden = document.querySelector(".header-hidden");
    var number_fit = document.querySelector(".header-box-1").children[1];
    var q = document;
    number_fit.addEventListener("mouseover", function () {
        header_hidden.style.display = "block";
    });
    q.addEventListener("click", function () {
        header_hidden.style.display = "none";
    });
    header_hidden.addEventListener("click", function (e) {
        e.stopPropagation();
    })
    // // 点击收货地址跳转
    var address_1=document.querySelector(".user-news-box-1-1");
    address_1.addEventListener("click",function(){
        window.location.href="/addressPage";
    });
    // 跳转到首页
    var all_main=document.querySelector(".nav-top-box-1").children[0]
    all_main.addEventListener("click", function () {
        window.location.href="/";
    });
    // 跳转到用户界面首页
    var main = document.querySelector(".header-box-1").children[0];
    main.addEventListener("click",function(){
        window.location.href="/User/userInterface"
    })// 跳转到登录页面或者个人中心
    var nav_top_box_2=document.querySelector(".nav-top-box-2").children[0];
    var text_1=nav_top_box_2.innerHTML;
    console.log(text_1)
    nav_top_box_2.addEventListener("click",function() {
        if (text_1 == "登录") {
            window.location.href = "/User/login";
        } else {
            window.location.href = "/User/userInterface";
        }
    })
    //点击退出跳转到主页
    var exit = document.querySelector(".exit")
    exit.addEventListener("click",function(){
        $.ajax({
            type: "post",
            url: "/User/loginOut",
            // data: {
            // },
            success: function (data) {
                // alert("退出")
                window.location.href="/";

            },error: function () {
                console.log("調取接口失敗")
            }
        })
    });
    // 点击跳转修改密码
    var change=document.querySelector(".changepassword");
    change.onclick=function () {
        window.location.href = "/User/updatePassword"
    }
    // 修改个人资料
    function keep(x) {
        var nickname = document.querySelector(".user-nickname")
        var age = document.querySelector(".user-age")
        var sex = document.querySelector(".user-sex")
        var phone = document.querySelector(".user-phone")
        var rName = document.querySelector(".user-rName")
        var idCard = document.querySelector(".user-idCard")
        var keep = document.querySelector(".keep");
        $.ajax({
            type: "post",
            url: "/User/queryUserId",
            data: {
                userId:x
            },
            success: function (data) {
                nickname.value = data.nickname==undefined?"":data.nickname;
                age.value = data.age==undefined?"":data.age;
                sex.value = data.sex==undefined?"":data.sex;
                phone.value = data.telephone==undefined?"":data.telephone;
                rName.value = data.rName==undefined?"":data.rName;
                idCard.value = data.idCard==undefined?"":data.idCard;
            }
        })
        keep.addEventListener("click", function () {
            if (phone.value.length !== 11||phone.value>0) {
                alert("手机格式有误")
            } else if (idCard.value.length !== 18 || idCard.value <= 0) {
                alert("身份证号有误")
            } else if(idCard.value>0||phone.value.length == 11||idCard.value.length == 18 || idCard.value > 0){
                $.ajax({
                    type: "post",
                    url: "/User/updateUser",
                    data: {
                        nickname: nickname.value,
                        age: age.value,
                        sex: sex.value,
                        telephone: phone.value,
                        rName: rName.value,
                        idCard: idCard.value,
                        userId:x
                    },
                    success: function (data) {
                        alert("修改成功")
                    }
                })
            }
        })
    }
    var userID=document.querySelector(".userID");
    if(userID.innerHTML=="用户名"){
        userID.style.display="none"
    }else {
        userID.style.display = "none"
        keep(userID.innerHTML);
    }
}